package com.organization.remoting.impl

import com.organization.remoting.DataService
import java.rmi.RemoteException
import org.springframework.beans.factory.annotation.Autowired
import com.organization.ServerApp

/**
 * Created with IntelliJ IDEA.
 * User: inester
 * Date: 09.07.12
 * Time: 9:24
 * To change this template use File | Settings | File Templates.
 */
class DataServiceImpl implements DataService {

    @Autowired
    def commonService
    @Autowired
    def treeService
    @Autowired
    def departmentService
    @Autowired
    def projectService
    @Autowired
    def employeeService

    //get all fields for organization node
    @Override
    Map getOrganizationInfo() throws RemoteException{
        def result = [:]
        result.putAll(commonService.goodResponse)
        result.putAt("name",ServerApp.getInstance().getSettings().getRootNodeStr())
        return result
    }

    //get all element fields by nodeId
    @Override
    Map getElement(String nodeId) throws RemoteException{
        if (nodeId==ServerApp.getInstance().getSettings().getRootNodeStr())
        {
            return getOrganizationInfo()
        }
        else if (nodeId=~commonService.depNodeStr)
        {
            return departmentService.getDepartmentInfo(nodeId)
        }
        else if (nodeId=~commonService.projNodeStr)
        {
            return projectService.getProjectInfo(nodeId)
        }
        else if (nodeId=~commonService.emplNodeStr)
        {
            return employeeService.getEmployeeInfo(nodeId)
        }
        return commonService.badResponse;
    }

    //update fields in node by nodeId
    @Override
    Map updateElement(Map params) throws RemoteException{
        def result = null
        if (params.nodeId=~commonService.depNodeStr)
        {
            result = departmentService.updateDepartmentInfo(params)
        }
        else if (params.nodeId=~commonService.projNodeStr)
        {
            result = projectService.updateProjectInfo(params)
        }
        else if (params.nodeId=~commonService.emplNodeStr)
        {
            result = employeeService.updateEmployeeInfo(params)
        }
        if (result){
            ServerApp.getInstance().sendMessage(params.nodeId,"update")
            return result
        }
        return commonService.badResponse;
    }

    //add new node to tree by parentNodeId
    @Override
    Map addElement(Map params) throws RemoteException{
        def result = null
        if (params.parentNodeId==ServerApp.getInstance().getSettings().getRootNodeStr())
        {
            result = departmentService.addDepartment(params)
        }
        else if (params.parentNodeId=~commonService.depNodeStr)
        {
            result = projectService.addProject(params)
        }
        else if (params.parentNodeId=~commonService.projNodeStr)
        {
            result = employeeService.addEmployee(params)
        }
        if (result){
            ServerApp.getInstance().sendMessage(params.parentNodeId,"add")
            return result
        }
        return commonService.badResponse;
    }

    //delete node from tree by nodeId
    @Override
    Map deleteElement(Map params) throws RemoteException{
        def result = null
        if (params.nodeId=~commonService.depNodeStr)
        {
            result = departmentService.deleteDepartment(params)
        }
        else if (params.nodeId=~commonService.projNodeStr)
        {
            result = projectService.deleteProject(params)
        }
        else if (params.nodeId=~commonService.emplNodeStr)
        {
            result = employeeService.deleteEmployee(params)
        }
        if (result){
            ServerApp.getInstance().sendMessage(params.nodeId,"delete")
            return result
        }
        return commonService.badResponse;
    }

    //get branch from selected node
    @Override
    List getBranch(String nodeId) throws RemoteException{
        if (nodeId == ServerApp.getInstance().getSettings().getRootNodeStr()){
            return treeService.getTree()
        }
        else if (nodeId =~ commonService.depNodeStr){
            return treeService.getProjectsTreeByDep(commonService.nodeIdToDbId(nodeId))
        }
        else if (nodeId =~ commonService.projNodeStr){
            return treeService.getEmployeeTreeByProj(commonService.nodeIdToDbId(nodeId))
        }
        return []
    }


}
